// 文字换行
const textWrap = (str, firstNum, twoNum) => {
    const len = str.length
    let strStart = str
    let strEnd = ''

    if (len > firstNum) {
        strStart = str.substring(0, firstNum)
        const stl = str.substring(firstNum, len)
        if (stl.length > twoNum) {
            strEnd = '...'.padStart(twoNum - 1, stl)
        } else {
            strEnd = stl
        }
    }
    return {
        strStart,
        strEnd
    }
}

const setShareImg = (options) => {
    return new Promise((resolve, rejcet) => {
        const {
            id,
            qrcode,
            userimg,
            username
        } = options
        const ctx = wx.createCanvasContext(id)
        // 设置背景为白色
        ctx.setFillStyle('#fff')
        ctx.fillRect(0, 0, 750, 1000)
        // 生成用户头像
        ctx.drawImage(userimg, 60, 60, 100, 100)
        ctx.setFontSize(34)
        // 生成标题
        ctx.fillStyle = '#000'

        const goodsTitle = textWrap(`${username}向你推荐好物`, 19, 14)
        // let textW = ctx.measureText(str).width / 4  // 设置文字居中
        ctx.fillText(goodsTitle.strStart, 200, 120)

        ctx.lineWidth = 2;
        ctx.beginPath();
        ctx.setLineDash([20, 5]);
        ctx.strokeStyle = '#ccc';
        ctx.moveTo(0, 210);
        ctx.lineTo(750, 210);
        ctx.stroke();

        // 广告语
        ctx.setFontSize(28)
        const bannerStr = '来康 一种健康的生活方式'
        ctx.fillText(bannerStr, 220, 750)
        // 生成二维码
        ctx.drawImage(qrcode, 150, 240, 450, 450)
        // 遮盖二维码底部文字
        // ctx.setFillStyle('#fff')
        // ctx.fillRect(488, 1105, 350, 60)
        // 绘制新的文字
        ctx.fillStyle = '#626262'
        ctx.setFontSize(20)
        // ctx.fillText('长按二维码进入小程序', 493, 1130)

        const imgurl = '/static/detail/share-img-bottom.jpg'
        ctx.drawImage(imgurl, 50, 780, 650, 160)
        console.log(options)

        ctx.draw(true, () => {
            console.log(options)
            wx.canvasToTempFilePath({
                width: 750,
                heght: 1000,
                destWidth: 750,
                destHeight: 1000,
                canvasId: id,
                fileType: 'jpg',
                quality: 1,
                success: (res) => {
                    resolve({
                        img: res.tempFilePath
                    })
                },
                fail: (e) => {
                    console.error('draw方法生成图片错误', e)
                    rejcet(e)
                },
            })
        })
    })
}

const init = (id, options = {}) => {
    let {
        qrUrl, // 小程序码地址
        userimg, // 用户头像
        username, // 用户昵称
    } = options
    // 缓存网络图片到本地
    let promise1 = new Promise(function(resolve, reject) {
        wx.getImageInfo({
            src: qrUrl,
            success: function(res) {
                // console.log(res)
                resolve(res);
            },
            fail: e => {
                console.error('缓存小程序码图片出现错误', e)
                reject(e)
            }
        })
    })
    let promise2 = new Promise(function(resolve, reject) {
        wx.getImageInfo({
            src: userimg,
            success: function(res) {
                // console.log(res)
                resolve(res)
            },
            fail: e => {
                console.error('缓存用户头像出现错误', e)
                reject(e)
            }
        })
    })

    let promiseAll = [promise1, promise2]
    return new Promise((reslove, reject) => {
        Promise.all(promiseAll).then(res => {
            let options = {
                id,
                qrcode: res[0].path,
                userimg: res[1].path,
                username
            }
            setShareImg(options).then(res => {
                reslove(res)
            }).catch(e => {
                reject(e)
            })
        }).catch(e => {
            console.error(e)
            reject(e)
        })
    })
}

export default init